begintownscript;

variables;
short bmessage,pcs,seated,leadchar,rctx,rcty,pchd,sp_no,abil_no;
string custom_m;

body;

beginstate INIT_STATE;

	set_total_visibility(0);
	if(get_flag(96,9) > 0)
		set_terrain(22,9,218);
	if(get_flag(97,9) > 0)
		set_terrain(22,11,218);
	if(get_flag(98,9) > 0)
		set_terrain(22,13,218);
	if(get_flag(99,9) > 0)
		set_terrain(22,15,218);

	set_char_dialogue_pic(7,532,0);

	set_flag(86,0,0);
	set_flag(87,0,0);

	if(get_flag(85,0) == 0){
//		set_total_visibility(1);
//		force_view_center(38,30);
//		rcty = 29;
//		while(rcty >= 9){
//			force_view_center(38,rcty);
//			force_instant_terrain_redraw();
//			pause(1);
//
//			rcty = (rcty - 1);
//		}
//
//		text_bubble_on_char(8,"Welcome.");
//		force_instant_terrain_redraw();
//		pause(8);
//
//		text_bubble_on_char(8,"");
//		force_instant_terrain_redraw();
//		reset_dialog();
//		add_dialog_str(0,"_Welcome to what I call the 'Doorless Room'._",0);
//		add_dialog_str(1,"_I must admit, you fared better than I had expected.  Your injury is unfortunate, but your decision to exterminate Footracer was both unexpected and incredibly beneficial to our cause._",0);
//		add_dialog_str(2,"_Killing Abidan's son was not expected.  His maiming would have been sufficient to scare Abidan into affording us more power, but his death seemed to push the General Field Marshall into shock._",0);
//		add_dialog_str(3,"_Still, I hope you see the plans I had set down?_",0);
//		add_dialog_choice(0,"...");
//		bmessage = run_dialog(0);
//
//		text_bubble_on_char(9,"The master...");
//		force_instant_terrain_redraw();
//		pause(8);
//
//		text_bubble_on_char(9,"");
//		force_instant_terrain_redraw();
//		reset_dialog();
//		add_dialog_str(0,"_Did the master authorize any of this?  I have not heard from him ever since we were banished.  When you suggested this plan to me, you made it sound as if it was his intention._",0);
//		add_dialog_choice(0,"...");
//		bmessage = run_dialog(0);
//
//		text_bubble_on_char(8,"The portal.");
//		force_instant_terrain_redraw();
//		pause(8);
//
//		text_bubble_on_char(8,"");
//		force_instant_terrain_redraw();
//		reset_dialog();
//		add_dialog_str(0,"_The portal remains closed, and he is on the other side.  However, he would not have identified us and had us banished if he did not have plans for us._",0);
//		add_dialog_str(1,"_He is still attempting to subvert Olindbar, I am sure of it.  He cannot be seen traveling to this side of the portal, however.  He came dangerously close to being banished himself, and coming here would likely condemn him._",0);
//		add_dialog_str(2,"_Thus, it is our job to finish his._",0);
//		add_dialog_choice(0,"...");
//		bmessage = run_dialog(0);
//
//		text_bubble_on_char(9,"What of me?");
//		force_instant_terrain_redraw();
//		pause(12);
//
//		text_bubble_on_char(9,"");
//		text_bubble_on_char(9,"What do I do now?");
//		force_instant_terrain_redraw();
//		pause(14);
//
//		text_bubble_on_char(9,"");
//		text_bubble_on_char(8,"Join me.");
//		force_instant_terrain_redraw();
//		pause(12);
//
//		text_bubble_on_char(8,"");
//		force_instant_terrain_redraw();
//		reset_dialog();
//		add_dialog_str(0,"_Your injury should be healed.  Stay here for a few days.  Think of a name and guise for yourself.  I will use my position to smuggle you into the military._",0);
//		add_dialog_str(1,"_I do not have any specific purpose for you, but if we still serve the same master, then you will follow my commands.  I know of a certain pest who will stand in our way._",0);
//		add_dialog_str(2,"_As we speak, he is consorting with some of his agents to dispense with a special unit one of our friends is assembling.  This pest is one blight on this planet we will have to fight with whatever power we amass._",0);
//		add_dialog_choice(0,"...");
//		bmessage = run_dialog(0);
//
//		text_bubble_on_char(9,"Very well.");
//		force_instant_terrain_redraw();
//		pause(12);
//
//		text_bubble_on_char(9,"");
//		text_bubble_on_char(9,"I will join you.");
//		force_instant_terrain_redraw();
//		pause(15);
//
//		text_bubble_on_char(9,"");
//		text_bubble_on_char(8,"Excellent.");
//		force_instant_terrain_redraw();
//		pause(13);
//
//		text_bubble_on_char(8,"");
//		force_instant_terrain_redraw();
//
//		rctx = 31;
//		while(rctx <= 45){
//			rcty = 2;
//			while(rcty <= 16){
//				pchd = (rctx - rcty);
//				while(pchd > 1){
//					pchd = (pchd - 2);
//				}
//				if(pchd == 0){
//					set_terrain(rctx,rcty,0);
//					set_floor(rctx,rcty,255);
//				}
//
//				rcty = (rcty + 1);
//			}
//			rctx = (rctx + 1);
//		}
//		force_instant_terrain_redraw();
//		pause(3);
//
//		rctx = 31;
//		while(rctx <= 45){
//			rcty = 2;
//			while(rcty <= 16){
//				set_terrain(rctx,rcty,0);
//				set_floor(rctx,rcty,255);
//
//				rcty = (rcty + 1);
//			}
//			rctx = (rctx + 1);
//		}
//		erase_char(8);
//		erase_char(9);
//		force_instant_terrain_redraw();
//		pause(3);

		leadchar = 0;
		while(char_ok(leadchar) == FALSE){
			leadchar = (leadchar + 1);
		}
		force_view_center(13,31);
		march_party(13,31);
		put_boom_on_space(13,31,2,0);
		set_character_facing(1000,0);
		set_total_visibility(0);
		force_instant_terrain_redraw();
		run_animation_sound(10);
		force_instant_terrain_redraw();

		if(party_size() > 1){
			force_view_center(13,30);
			march_party(13,30);
			put_boom_on_space(13,31,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
		}

		if(party_size() > 2){
			force_view_center(13,29);
			march_party(13,29);
			put_boom_on_space(13,31,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
		}

		if(party_size() > 3){
			force_view_center(13,28);
			march_party(13,28);
			put_boom_on_space(13,31,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
		}

		reset_dialog();
		if(party_size() == 1){
			add_dialog_str(0,"Goddamn, I will never get used to coming here.  It's far too painful to be worth the effort.  What the hell is Katothen thinking?",0);
			add_dialog_str(1,"Anyway, this appears to be a different Sanctuary, or a different part of it.  At least-- or so I hope-- there won't be any more brigands here.",0);
		}
		if(party_size() > 1){
			add_dialog_str(0,"Goddamn, we will never get used to coming here.  It's far too painful to be worth the effort.  What the hell is Katothen thinking?",0);
			add_dialog_str(1,"Anyway, this appears to be a different Sanctuary, or a different part of it.  At least-- or so we hope-- there won't be any more brigands here.",0);
		}
		add_dialog_choice(0,"After all, it's called 'Sanctuary'...");
		bmessage = run_dialog(1);

		set_flag(85,0,1);
		block_entry(1);
	}

break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;

	if(get_terrain(13,26) != 30){
		set_terrain(13,26,30);
		march_party(13,26);
		force_view_center(13,26);
		force_instant_terrain_redraw();
		play_sound(99);
		block_entry(1);
	}

break;

beginstate 11;

	if(get_flag(85,0) < 2){
		march_party(13,25);
		force_view_center(13,25);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(3);

		reset_dialog();
		add_dialog_str(0,"There is a mage in purple here.  What does he want?",0);
		add_dialog_str(1,"_Hello...  Sorry, but I don't have much time.  Hunter here will tell you more, but I want to say this much:_",0);
		add_dialog_str(2,"_I am Katothen still.  Or at least, a simulacrum of him.  Half of him, maybe?  Regardless, it is I with whom you spoke last, and it is I who saved you while fighting this 'The Immortal'._",0);
		add_dialog_str(3,"_I have to go to a meeting soon with the remaining six generals and my other alter-ego, Janus.  Who you see me now as is 'Orphus', Penultimate Consul of the Imperial Magisterium.  Janus is the Ultimate Consul of the same._",0);
		if(get_flag(72,0) >= 1)
			add_dialog_str(4,"_Talk with Hunter.  She will tell you what we have concluded._",0);
		else
			add_dialog_str(4,"_The woman sitting here is named Hunter.  Talk with her.  She will tell you what we have concluded._",0);
		if(get_flag(82,0) < 2){
			add_dialog_choice(0,"Very well.");
		}
		else{
			if(party_size() == 1)
				add_dialog_choice(0,"Oh!  I saw that nexus you were talking about.");
			else
				add_dialog_choice(0,"Oh!  We saw that nexus you were talking about.");
			bmessage = run_dialog(1);

			reset_dialog();
			add_dialog_str(0,"_Yes, well...  at least you now know why I have to be so cautious.  That nexus could awaken at any moment, and if it does, it's disaster for all of us._",0);
			add_dialog_str(1,"_Just stay away from it.  It's not safe.  But...  I do have to go, so I will see you later._",0);
			add_dialog_choice(0,"Very well.");
		}
		bmessage = run_dialog(1);

		erase_char(6);
		put_boom_on_space(13,21,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);

		block_entry(1);
		set_flag(85,0,2);
	}

break;

beginstate 12;

	if(get_flag(88,0) == 0){
		message_dialog("This room contains a large grid of runes.  They look fairly delicate, though-- delicate enough that merely stepping on it would cause it to dissipate.","Suddenly, a voice booms out, as if from beyond the Blades of Avernum Editor: _Write my name!_  What does he mean, though?");
		set_flag(88,0,1);
	}

break;

beginstate 13;

	if(get_flag(86,0) >= 22){
		teleport_party(7,42,0);
		if(get_flag(1,1) == 0){
			reset_dialog();
			add_dialog_str(0,"The voice from behind the BoA Editor speaks up again.  _Wow!  Well played.  Wasn't expecting you to get that one.  Anyway, consider this my apology for making an Echoes scenario without any Illithids to be killed._",0);
			add_dialog_str(1,"_Anyway, here's a Flayer's Flail.  It's, I dunno.  Maybe you can whack people with it?  Who knows.  Just take it._",0);
			add_dialog_choice(0,"What's an Illithid, anyway?");
			bmessage = run_dialog(1);
			set_flag(1,1,1);
		}
	}

break;

beginstate 14;

	teleport_party(11,10,0);

break;

beginstate 15;

	if(get_flag(96,9) == 0){
		reset_dialog();
		add_dialog_str(0,"Spell: TRANSFER ENERGY",40);
		add_dialog_str(1,"This spell costs 33% of a caster's current SP total and gives 25% of the caster's SP to a character of her/his choosing.",0);
		sp_no = 0;
		if(get_flag(97,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(98,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(99,9) > 0)
			sp_no = (sp_no + 1);
		add_dialog_choice(0,"Leave it alone.");
		if(sp_no >= 2){
			if(party_size() == 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If I want to take it, I will have to set one of the two spellbooks I already have back down on their pedestals.",0);
			if(party_size() > 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If we want to take it, we will have to set one of the two spellbooks we already have back down on their pedestals.",0);
		}
		else{
			add_dialog_choice(1,"Take it.");
		}
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE)
				change_custom_abil_uses(pcs,0,1);

			pcs = (pcs + 1);
		}
		set_terrain(22,9,218);
		change_spec_item(17,1);
		set_flag(96,9,1);
	}
	else{
		reset_dialog();
		add_dialog_str(0,"This is where the _Transfer Energy_ spellbook used to lie.",0);
		add_dialog_choice(0,"Leave it alone.");
		add_dialog_choice(1,"Put the spellbook back.");
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			abil_no = get_custom_abil_uses(pcs,0);
			change_custom_abil_uses(pcs,0,(abil_no * -1));

			pcs = (pcs + 1);
		}
		set_terrain(22,9,217);
		change_spec_item(17,(has_special_item(17) * -1));
		set_flag(96,9,0);
	}

break;

beginstate 16;

	if(get_flag(97,9) == 0){
		reset_dialog();
		add_dialog_str(0,"Spell: ROOST",40);
		add_dialog_str(1,"This spell costs 12 spell points and raises the ground below the caster for the duration of two turns, protecting the caster from melee attackers and giving resistance to the harmful effects of ranged attackers while roosted.",0);
		sp_no = 0;
		if(get_flag(96,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(98,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(99,9) > 0)
			sp_no = (sp_no + 1);
		add_dialog_choice(0,"Leave it alone.");
		if(sp_no >= 2){
			if(party_size() == 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If I want to take it, I will have to set one of the two spellbooks I already have back down on their pedestals.",0);
			if(party_size() > 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If we want to take it, we will have to set one of the two spellbooks we already have back down on their pedestals.",0);
		}
		else{
			add_dialog_choice(1,"Take it.");
		}
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE)
				change_custom_abil_uses(pcs,1,1);

			pcs = (pcs + 1);
		}
		set_terrain(22,11,218);
		change_spec_item(18,1);
		set_flag(97,9,1);
	}
	else{
		reset_dialog();
		add_dialog_str(0,"This is where the _Roost_ spellbook used to lie.",0);
		add_dialog_choice(0,"Leave it alone.");
		add_dialog_choice(1,"Put the spellbook back.");
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			abil_no = get_custom_abil_uses(pcs,1);
			change_custom_abil_uses(pcs,1,(abil_no * -1));

			pcs = (pcs + 1);
		}
		set_terrain(22,11,217);
		change_spec_item(18,(has_special_item(18) * -1));
		set_flag(97,9,0);
	}

break;

beginstate 17;

	if(get_flag(98,9) == 0){
		reset_dialog();
		add_dialog_str(0,"Spell: FRACAS",40);
		add_dialog_str(1,"This spell sends the caster and her/his party into a berserker rage, giving heavier benefits to spellcasters, allowing the party to wreak mindless havoc on the enemies around them at the cost of 30 spell points.",0);
		sp_no = 0;
		if(get_flag(96,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(97,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(99,9) > 0)
			sp_no = (sp_no + 1);
		add_dialog_choice(0,"Leave it alone.");
		if(sp_no >= 2){
			if(party_size() == 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If I want to take it, I will have to set one of the two spellbooks I already have back down on their pedestals.",0);
			if(party_size() > 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If we want to take it, we will have to set one of the two spellbooks we already have back down on their pedestals.",0);
		}
		else{
			add_dialog_choice(1,"Take it.");
		}
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE)
				change_custom_abil_uses(pcs,2,1);

			pcs = (pcs + 1);
		}
		set_terrain(22,13,218);
		change_spec_item(19,1);
		set_flag(98,9,1);
	}
	else{
		reset_dialog();
		add_dialog_str(0,"This is where the _Fracas_ spellbook used to lie.",0);
		add_dialog_choice(0,"Leave it alone.");
		add_dialog_choice(1,"Put the spellbook back.");
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			abil_no = get_custom_abil_uses(pcs,2);
			change_custom_abil_uses(pcs,2,(abil_no * -1));

			pcs = (pcs + 1);
		}
		set_terrain(22,13,217);
		change_spec_item(19,(has_special_item(19) * -1));
		set_flag(98,9,0);
	}

break;

beginstate 18;

	if(get_flag(99,9) == 0){
		reset_dialog();
		add_dialog_str(0,"Spell: ESUNA",40);
		add_dialog_str(1,"This spell purges the party of both positive and negative status effects, but also temporarily prevents the harmful status alterations of enemy archers and monks at the cost of 20 spell points.",0);
		sp_no = 0;
		if(get_flag(96,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(97,9) > 0)
			sp_no = (sp_no + 1);
		if(get_flag(98,9) > 0)
			sp_no = (sp_no + 1);
		add_dialog_choice(0,"Leave it alone.");
		if(sp_no >= 2){
			if(party_size() == 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If I want to take it, I will have to set one of the two spellbooks I already have back down on their pedestals.",0);
			if(party_size() > 1)
				add_dialog_str(2,"Unfortunately, it's a large spellbook.  If we want to take it, we will have to set one of the two spellbooks we already have back down on their pedestals.",0);
		}
		else{
			add_dialog_choice(1,"Take it.");
		}
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE)
				change_custom_abil_uses(pcs,3,1);

			pcs = (pcs + 1);
		}
		set_terrain(22,15,218);
		change_spec_item(20,1);
		set_flag(99,9,1);
	}
	else{
		reset_dialog();
		add_dialog_str(0,"This is where the _Esuna_ spellbook used to lie.",0);
		add_dialog_choice(0,"Leave it alone.");
		add_dialog_choice(1,"Put the spellbook back.");
		bmessage = run_dialog(0);
		if(bmessage == 1)
			end();
		pcs = 0;
		while(pcs <= 3){
			abil_no = get_custom_abil_uses(pcs,3);
			change_custom_abil_uses(pcs,3,(abil_no * -1));

			pcs = (pcs + 1);
		}
		set_terrain(22,15,217);
		change_spec_item(20,(has_special_item(20) * -1));
		set_flag(99,9,0);
	}

break;

beginstate 19;

	if(get_flag(85,0) == 10){
		march_party(18,0);
		put_boom_on_space(13,32,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);
		pause(4);

		pcs = 1;
		while(party_size() > pcs){
			march_party(18,0);
			put_boom_on_space(13,32,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
			pause(4);

			pcs = (pcs + 1);
		}

		set_flag(83,0,10);
		move_to_new_town(18,18,16);
	}

break;

beginstate 30;

	reset_dialog_preset_options(4);
	bmessage = run_dialog(0);
	if(bmessage == 1)
		end();
	play_sound(37);
	set_terrain(8,10,270);
	rctx = 17;
	while(rctx <= 25){
		rcty = 31;
		while(rcty <= 35){
			if((get_ran(1,0,1) == 1) && (get_terrain(rctx,rcty) == 0))
				put_effect_on_space(rctx,rcty,10,1,1);
			set_terrain(rctx,rcty,270);

			rcty = (rcty + 1);
		}

		rctx = (rctx + 1);
	}
	force_instant_terrain_redraw();
	run_animation_sound(25);

	set_flag(86,0,0);
	set_flag(87,0,0);

	message_dialog("It's that voice again, the voice from beyond the Blades of Avernum Editor:","_Write my name!_");

break;

